package org.hw.algorithm.common;

import org.hw.algorithm.LinkedList.LinkNode;

import java.util.Random;

public class CommonUtils {
    public static void swap(char[] chars,int i,int j){
        char tmp=chars[i];
        chars[i]=chars[j];
        chars[j]=tmp;
    }
    public static void swapInt(int[] arr,int i,int j){
        int tmp=arr[i];
        arr[i]=arr[j];
        arr[j]=tmp;
    }

    public static void swap(int[] arr,int left,int right){
        int temp=arr[left];
        arr[left]=arr[right];
        arr[right]=temp;
    }
    public static int[] createArray(int blow, int size) {
        int[] array = new int[size];
        Random random = new Random();
        for (int i = 0; i < size; i++) {
            array[i] = random.nextInt(blow);
        }
        return array;
    }

    public static void printArray(int[] arr){
        for (int i = 0; i < arr.length;i++){
            System.out.print(i);
        }
        System.out.println();
    }


    public static LinkNode createLink(int length, boolean haveHead) {
        LinkNode head = null;
        if (haveHead) {
            head = new LinkNode(0);
            LinkNode node = new LinkNode(new Random().nextInt(length));
            head.next = node;
            int i = 1;
            while (i <= length) {
                LinkNode next = new LinkNode(new Random().nextInt(length));
                node.next = next;
                node = next;
                i++;
            }
            ;

        } else {
            LinkNode node = new LinkNode(new Random().nextInt(length));
            head = node;
            int i = 1;
            while (i <= length) {
                LinkNode next = new LinkNode(new Random().nextInt(length));
                node.next = next;
                node = next;
                i++;
            }
        }
        return head;
    }

    public static void printLink(LinkNode head,boolean haveHead) {
        if (head == null) return;
        LinkNode temp = haveHead ? head.next : head;
        do {
            System.out.print(temp.value+" ");
            temp = temp.next;
        } while (temp != null);
        System.out.println();
    }
    public static void printLink(LinkNode head) {
        CommonUtils.printLink(head,true);
    }

    public static boolean isEmptyLink(LinkNode head , boolean haveHead ) {
        if (haveHead){
            return head.next ==null;
        }else{
            return head ==null;
        }
    }
}
